<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of menuAction
 * 游戏物品类型管理
 * @author skyinter
 */
class stdmodeAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    

    /**
     * 物品类型列表
     */
    public function index() {
        $page = $this->_get('page') ? $this->_get('page') : '1';
        // $keyword = $this->_get('keyword') ? $this->_get('keyword') : '';
        $game_id = $this->_getid('game_id' , 0);
        $where = '1=1';
        if($game_id) {
            $where .= " AND game_id = ".$game_id;
        } 
        $list = D('stdmode')->where($where)->page($page , 20)->order('`sort`')->findAll();
        $total = D('stdmode')->getTotal();
        if($list) {
            foreach ($list as $k => &$v) {
                $game = M('game')->field('name')->where(array('id' => $v['game_id']))->find();
                $v['game'] = $game['name'];
            }
        }
        //游戏
        $games = M('game')->findAll();
        $this->assign('list', $list);
        $this->assign('total', $total);
        $this->assign('page', $page);
        // $this->assign('keyword', $keyword);
        $this->assign('games' , $games);
        $this->assign('gameid' , $game_id);
        $this->display();
    }


    public function add() {
        if ($_POST) {
            $data['game_id'] = $this->_postid('game_id' , 0);
            $data['name'] = $this->_post('name');
            $data['abridge'] = $this->_post('abridge');
            $data['unit'] = $this->_post('unit');
            $data['sort'] = $this->_post('sort');
            $data['is_show'] = $this->_post('is_show');

            //判断是否存在
            $info = D('stdmode')->where(array('game_id' => $data['game_id'],'name' => $data['name']))->find();
            if($info) {
                $this->JsonReturn('该游戏下物品类型已存在' , null , 0);
            }
            $result = D('stdmode')->insert($data);
            if ($result > 0) {
                $this->JsonReturn('添加成功', null, 1);
            } else {
                $this->JsonReturn('添加失败', null, 0);
            }
            exit;
        }

        $games = M('game')->findAll();
        $this->assign('games' , $games);
        $this->display();
    }


    public function edit() {
        if ($_POST) {
            $id = $this->_postid('id');
            $data['game_id'] = $this->_postid('game_id' , 0);
            $data['name'] = $this->_post('name');
            $data['abridge'] = $this->_post('abridge');
            $data['unit'] = $this->_post('unit');
            $data['sort'] = $this->_post('sort');
            $data['is_show'] = $this->_post('is_show');
            $result = D('stdmode')->update($data , array('id'=>$id));
            if ($result > 0) {
                $this->JsonReturn('编辑成功', null, 1);
            } else {
                $this->JsonReturn('编辑失败', null, 0);
            }
            exit;
        }
        $id = $this->_getid('id' , 0);
        if(!$id) {
            showError('参数丢失');
        }
        $data = M('stdmode')->where(array('id' => $id))->find();
        $games = M('game')->findAll();
        $this->assign('games' , $games);
        $this->assign('data' , $data);
        $this->display();
    }

    public function ajax() {
        $id = $this->_get('id') ? $this->_get('id') : null;
        $type = $this->_get('type') ? $this->_get('type') : null;

        switch ($type) {
            case 'down': //商品下架
                $data = array('state' => 2);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'del': //商品删除
                $data = array('is_del' => 1);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'up': //商品上架
                $data = array('state' => 1);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'rateSave': //商品类别名称更改
                $data = array('name' => $this->_get('name'),'is_show' => $this->_get('is_show',0), 'sort' => $this->_get('sort'));
                $stat = M('rate')->update($data , array('id' => $id));
                break;
            case 'typeDel': //分类
                $data = array('id' => $id);
                $stat = D('stdmode')->delete($data);
                break;
            default:
                $stat = 0;
                break;
        }

        if ($stat > 0) {
            $info = '请求成功';
            $this->JsonReturn($info, null, 1);
        } else {
            $info = '请求失败';
            $this->JsonReturn($info, null, 0);
        }
    }

}
